const global ={}

// 获取评论列表
function getComments(page) {
    request({
        type: 'get',
        url: '/admin/comment/search',
        data: {page},
        success: (res) => {
            if (res.code === 200) {
                const renderResult = template('commentList', res.data)
                $('tbody').html(renderResult)
                initPagination(res.data.totalPage)
            }
        }
    })
}
getComments()

// 分页
function initPagination(totalPages) {
    $('.pagination').twbsPagination({
      totalPages, 
      visiblePages: 5, 
      first:'首页',
      last:'末页',
      prev:'上一页',
      next:'下一页',
      onPageClick:  (event, page) => {
        this.getComments(page);
        global.page = page
      }
    });
}
  
// 删除评论
$('tbody').on('click', '.delete', function () {
  const id = $(this).attr('data-id')
  request({
    type: 'post',
    url: '/admin/comment/delete',
    data: { id },
    success: (res) => {
      if(res.code === 200){
        alert(res.msg)
        getComments(global.page)
      }
    }
  })
})

// 拒绝
$('tbody').on('click', '.reject', function () {
  const id = $(this).attr('data-id')
  request({
    type: 'post',
    url: '/admin/comment/reject',
    data: { id },
    success: (res) => {
      console.log(res)
      if(res.code === 200){
        alert(res.msg)
        getComments(global.page)
      }
    }
  })
})

// 批准
$('tbody').on('click', '.allow', function () {
  const id = $(this).attr('data-id')
  request({
    type: 'post',
    url: '/admin/comment/pass',
    data: { id },
    success: (res) => {
      console.log(res)
      if(res.code === 200){
        alert(res.msg)
        getComments(global.page)
      }
    }
  })
})